package pers.mine.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pers.mine.po.GoodsGroup;
import pers.mine.po.GoodsItem;
import pers.mine.po.GoodsItemExample;
import pers.mine.service.inter.GoodsGroupService;
import pers.mine.service.inter.GoodsItemService;
import pers.mine.service.inter.GroupShipService;
import pers.mine.utils.SimpleUtil;
import pers.mine.vo.Info;
import pers.mine.vo.PageInfo;

/**
 * 商品-商品组映射 Controller
 * 
 * @author Mine
 * @since 2016年11月28日_下午4:55:56
 */
@Controller
@RequestMapping("/groupShip")
public class AdminGroupShipController {
	// 展示页面-按商品查看[商品id,name,类型,]
	private String pageTitle = "商品管理";
	private Info indexinfo = new Info();// 放到首页的信息
	@Autowired
	GoodsItemService goodsItemService;
	@Autowired
	GroupShipService groupShipService;
	@Autowired
	GoodsGroupService goodsGroupService;
	String onePath = "admin/groupship/";

	/**
	 * 依据分页信息,关键字信息 获得商品列表,并展示到后台商品页
	 */
	@RequestMapping(value = "/show")
	public String show(Integer nowPage, Integer pageSize, Integer pageCount,
			String keys, Model model, HttpServletRequest request) {
		GoodsItemExample exam = new GoodsItemExample();

		if (pageCount != null && nowPage != null && pageCount < nowPage) {
			nowPage = pageCount;
		}
		exam.initLimit(nowPage, pageSize);
		if (keys != null && !"".equals(keys.trim())) {
			// 关键字写回页面
			model.addAttribute("keys", keys);

			String keysPro = "%" + keys + "%";
			// 写死了。。。
			exam.or().andNameLike(keysPro);// 商品名
			exam.or().andOriginLike(keysPro);// 产地
			exam.or().andDescLike(keysPro);// 简介

		}
		// 排序方式
		exam.setOrderByClause("g_id,g_name");
		// 依据筛选条件获取
		PageInfo<GoodsItem> pi = goodsItemService.findPageByExample(exam);

		// 处理商品类型字段
		List<GoodsItem> targetList = groupShipService.fillTypeForGoodsList(pi
				.getBasePageList());
		// 设置
		pi.setBasePageList(targetList);

		// 商品基本类型List获取
		List<GoodsGroup> simpleGroups = goodsGroupService.findBySimpleType();
		// 商品汽车类型List获取
		List<GoodsGroup> carGroups = goodsGroupService.findByCarType();
		Info temp = SimpleUtil.getIndexInfoWithRequest(request);
		if (temp != null) {
			indexinfo = temp;
		}
		// 判断是否可以放入信息
		if (indexinfo.isCanChange()) {
			indexinfo.setType(Info.SUCCESS);
			if (keys != null && !"".equals(keys.trim())) {
				indexinfo.setMsg("[商品类型查看]:对于关键字[" + keys + "]查询商品成功...");
			} else {
				indexinfo.setMsg("[商品类型查看]:查询所有商品成功...");
			}
		} else {
			indexinfo.setCanChange(true);
		}

		model.addAttribute("pageTitle", pageTitle);
		model.addAttribute("indexinfo", indexinfo);
		model.addAttribute("goodsPageInfo", pi);
		// 商品组
		model.addAttribute("simpleGroups", simpleGroups);
		model.addAttribute("carGroups", carGroups);
		return onePath + "groupShipPage";
	}

	// 执行组修改
	@RequestMapping(value = "/edit")
	public String editBaseInfo(Integer goodsId, Integer[] groupids, Model model) {
		System.out.println(Arrays.toString(groupids));
		// 校验id
		if (goodsId == null) {
			indexinfo.setType(Info.ERROR);
			indexinfo.setMsg("[商品类型设置]:商品id未指定...");
		} else {
			// 执行修改操作
			if (groupShipService.addGroupsForGoods(goodsId, groupids)) {// 成功
				indexinfo.setType(Info.SUCCESS);
				indexinfo.setMsg("[商品类型设置]:设置#" + goodsId + "号商品类型成功...");
			} else {// 失败
				indexinfo.setType(Info.ERROR);
				indexinfo.setMsg("[商品类型设置]:修改#" + goodsId + "号商品类型失败,重新修改...");
			}
		}
		indexinfo.setCanChange(false);
		model.addAttribute("indexinfo", indexinfo);
		return "forward:show";
	}
}
